package com.kanesoft.android.gdx.actors.fight;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.kanesoft.android.gdx.actors.ActorObject;
import com.kanesoft.android.gdx.control.WorldControl;
import com.kanesoft.android.gdx.widgets.GdxWidgets;
import com.kanesoft.android.gdx.widgets.sprites.SpriteInfo;
import com.kanesoft.android.gdx.widgets.sprites.SpriteSlideInfo;
import com.kanesoft.android.gdx.widgets.sprites.SpriteWidget;

public class PowerFireballActor extends FightActorObject {

    private SpriteInfo siFireballFlying;
    private SpriteInfo siFireballExploding;

    private ActorObject originalActor;

    private final float FIREBALL_SPEED = 6f;
    
    private PowerFireballFlying fireballFlying;
    private PowerFireballExploding fireballExploding;
    

    public PowerFireballActor(float x, float y, float scale, WorldControl worldCtrl, ActorObject originalActor) {
        super(scale, worldCtrl);

        this.originalActor = originalActor;
        
        Vector2 pixelPos = worldControl.convertWorldPointToPixel(x, y);
        actorSpriteWidget = new SpriteWidget(pixelPos.x, pixelPos.y, scale, scale, null, this);
        
        siFireballFlying = new SpriteInfo("data/bride.png", 0.150f);
        siFireballFlying.addSpriteSlide(new SpriteSlideInfo(120*4, 624, 95, 65, true));
        siFireballFlying.addSpriteSlide(new SpriteSlideInfo(120*4, 624+65, 95, 45, true));
        fireballFlying = new PowerFireballFlying(siFireballFlying, this);

        siFireballExploding = new SpriteInfo(siFireballFlying, 0.100f);
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(120*4, 624+65+45, 95, 70));
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(120*5, 624, 80, 65));
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(120*5, 624+65, 55, 65));
        fireballExploding = new PowerFireballExploding(siFireballExploding, this);
        
        /*
        siFireballFlying = new SpriteInfo("data/street_fighter_ryu_cian_1024x1024.png", 0.150f);
        siFireballFlying.addSpriteSlide(new SpriteSlideInfo(528, 540, 554 - 528, 566 - 540, true));
        siFireballFlying.addSpriteSlide(new SpriteSlideInfo(558, 543, 589 - 558, 566 - 543, true));
        fireballFlying = new PowerFireballFlying(siFireballFlying, this);

        siFireballExploding = new SpriteInfo(siFireballFlying, 0.150f);
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(594, 544, 603 - 594, 566 - 544));
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(606, 540, 623 - 606, 566 - 540));
        siFireballExploding.addSpriteSlide(new SpriteSlideInfo(628, 540, 652 - 628, 566 - 540));
        fireballExploding = new PowerFireballExploding(siFireballExploding, this);
        */
        actorSpriteWidget.enabled = false;
        
        setHorizontalFlip(originalActor.getHorizontalFlip());
        setActorState(fireballFlying);
    }

    @Override
    public void updateActorPositions() {
        Vector2 widgetPos = getPosition();
        float deltaTime = Gdx.graphics.getDeltaTime();
        
        if (getActorState().getStateName().equalsIgnoreCase("FireballExploding")) return;

        float signal = 1;
        if (getHorizontalFlip()) signal = -1;

        Vector2 widgetProjection = new Vector2(worldControl.convertPixelXGLPosX((int) widgetPos.x),
                worldControl.convertPixelYGLPosY((int) widgetPos.y));

        widgetProjection.x = widgetProjection.x + (signal * FIREBALL_SPEED * deltaTime);

        Vector2 pixelPos = worldControl.convertWorldPointToPixel(widgetProjection.x, widgetProjection.y);
        setPosition(Math.round(pixelPos.x), Math.round(pixelPos.y));
    }

    private void doActionExploding() {
        setActorState(fireballExploding);
    }

    @Override
    public void onClick() {
        // TODO Auto-generated method stub
    }

    @Override
    public void onAnimationComplete() {
        actorSpriteWidget.enabled = false;
    }

    @Override
    public void actualAttackBehavior(boolean attackBehavior) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setPosition(int x, int y) {
        actorSpriteWidget.setSpritePosition(x, y);
    }

    @Override
    public Vector2 getPosition() {
        return new Vector2(actorSpriteWidget.getPoxX(), actorSpriteWidget.getPoxY());
    }

    @Override
    public GdxWidgets getActorWidget() {
        return actorSpriteWidget;
    }

    @Override
    public void handleActorsCollision(ActorObject collidingActor) {
        if (originalActor.equals(collidingActor)) return;
        if (getActorState().getStateName().equalsIgnoreCase("FireballExploding")) return;

        doActionExploding();
    }

    @Override
    public boolean isAttacking(ActorObject attacked) {
        if (originalActor.equals(attacked)) return false;

        return true;
    }

    @Override
    public float getHitAceleration() {
        return 0;
    }

    public void setHorizontalFlip(boolean flip) {
        super.setHorizontalFlip(flip);
        actorSpriteWidget.setFlipSpriteHorizontal(flip);
    }

    @Override
    public void onSpriteOutOfScreen() {
        actorSpriteWidget.enabled = false;
        
    }
    
    
    public boolean canThrowFireball() {
        return !actorSpriteWidget.enabled;
    }
    
    public void throwFireball(float x, float y) {
        if (canThrowFireball() == false) return;
        Vector2 pixelPos = worldControl.convertWorldPointToPixel(x, y);
        actorSpriteWidget.setSpritePosition(pixelPos.x, pixelPos.y);
        
        
        setHorizontalFlip(originalActor.getHorizontalFlip());
        setActorState(fireballFlying);
        
        actorSpriteWidget.enabled = true;
    }

    @Override
    public void notifyLifeIsGone() {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void initializeMoviments() {
        // TODO Auto-generated method stub
        
    }

}
